/*****************************************************************************

  The following code is derived, directly or indirectly, from the SystemC
  source code Copyright (c) 1996-2006 by all Contributors.
  All Rights reserved.

  The contents of this file are subject to the restrictions and limitations
  set forth in the SystemC Open Source License Version 2.4 (the "License");
  You may not use this file except in compliance with such restrictions and
  limitations. You may obtain instructions on how to receive a copy of the
  License at http://www.systemc.org/. Software distributed by Contributors
  under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
  ANY KIND, either express or implied. See the License for the specific
  language governing rights and limitations under the License.

 *****************************************************************************/

/*****************************************************************************

  sc_object.h -- Abstract base class of all SystemC `simulation' objects.

  Original Author: Stan Y. Liao, Synopsys, Inc.

 *****************************************************************************/

/*****************************************************************************

  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
  changes you are making here.

      Name, Affiliation, Date: Andy Goodrich, Forte Design Systems
                               5 September 2003
  Description of Modification: - Made creation of attributes structure      
                                 conditional on its being used. This eliminates
                                 100 bytes of storage for each normal sc_object.

 *****************************************************************************/

// $Log: sc_object.h,v $
// Revision 1.1.1.1  2006/12/15 20:31:37  acg
// SystemC 2.2
//
// Revision 1.4  2006/04/11 23:13:21  acg
//   Andy Goodrich: Changes for reduced reset support that only includes
//   sc_cthread, but has preliminary hooks for expanding to method and thread
//   processes also.
//
// Revision 1.3  2006/01/13 18:44:30  acg
// Added $Log to record CVS changes into the source.
//

#ifndef SC_OBJECT_H
#define SC_OBJECT_H


#include "sysc/utils/sc_iostream.h"
#include "sysc/kernel/sc_attribute.h"

namespace sc_core {

class sc_trace_file;
class sc_simcontext;


// ----------------------------------------------------------------------------
//  CLASS : sc_object
//
//  Abstract base class of all SystemC `simulation' objects.
// ----------------------------------------------------------------------------


/*
 *  The graph of sc_objects may be traversed; an sc_object may be shared.
 *
 *
 */

class sc_object 
{
    friend class sc_object_manager;
	friend class sc_module_dynalloc_list;
	friend class sc_process_b;

public:

	/*
	 *  *name() methods are MT-safe
	 */
    const char* name() const
        { return m_name; }
    const char* basename() const;


    /*
     *  Might cause a drc on os, if the same stream is used
     */
    virtual void print(::std::ostream& os=::std::cout ) const;
    // dump() is more detailed than print()
    virtual void dump(::std::ostream& os=::std::cout ) const;


    virtual void trace( sc_trace_file* tf ) const;			//TODO check sc_trace_file for drc's

    virtual const char* kind() const { return "sc_object"; }

    sc_simcontext* simcontext() const			//exposes pointer to global object
        { return m_simc; }

    // add attribute
    /*
     * Allowed at sim-time directly, drc on m_attr_cltn_p, need to synch
     * The pointed sc_attr_cltn is also not MT-safe
     *
     */
    bool add_attribute( sc_attr_base& );

    // get attribute by name
    /*
     * Allowed at sim-time directly, drc on m_attr_cltn_p, need to synch
     * The pointed sc_attr_cltn is also not MT-safe
     *
     */
          sc_attr_base* get_attribute( const std::string& name_ );
    const sc_attr_base* get_attribute( const std::string& name_ ) const;

    // remove attribute by name
    /*
     * Allowed at sim-time directly, drc on m_attr_cltn_p, need to synch
     * The pointed sc_attr_cltn is also not MT-safe
     *
     */
    sc_attr_base* remove_attribute( const std::string& name_ );

    // remove all attributes
    /*
     * Allowed at sim-time directly, drc on m_attr_cltn_p, need to synch
     * The pointed sc_attr_cltn is also not MT-safe
     *
     */
    void remove_all_attributes();

    // get the number of attributes
    /*
     * Allowed at sim-time directly, drc on m_attr_cltn_p, need to synch
     * The pointed sc_attr_cltn is also not MT-safe
     *
     */
    int num_attributes() const;

    // get the attribute collection
    /*
     * Allowed at sim-time directly, drc on m_attr_cltn_p, need to synch
     * The pointed sc_attr_cltn is also not MT-safe
     *
     */
          sc_attr_cltn& attr_cltn();
    const sc_attr_cltn& attr_cltn() const;

    /*
     * MT-safe here, need to check overrides
     *
     */
    virtual const std::vector<sc_object*>& get_child_objects() const
        { return *(new std::vector<sc_object*>); }

    sc_object* get_parent() const { return m_parent; } 
    sc_object* get_parent_object() const { return m_parent; }

protected:

    sc_object();
    sc_object(const char* nm);
    virtual ~sc_object();

private:

    void sc_object_init(const char* nm);

private:

    /* Each simulation object is associated with a simulation context */ 
    sc_simcontext*         m_simc;				//points to global data, not MT-safe (however, m_simc itself is MT-safe, because it is thread-local)
    char*                  m_name;				//MT-safe, read-only after the init.
    mutable sc_attr_cltn*  m_attr_cltn_p;		//not MT-safe, r/w accesses, the pointer itself is also not MT-safe
    sc_object*             m_parent;			//points to shared data, may cause drc on the parent object, need to check it
};


// ----------------------------------------------------------------------------

extern const char SC_HIERARCHY_CHAR;
extern bool sc_enable_name_checking;


inline 
sc_object* sc_get_parent( const sc_object* obj_p ) 
{ 
	return obj_p->get_parent_object(); 
}

} // namespace sc_core

#endif // SC_OBJECT_H
